package com.dk.controller;

import com.dk.data.Result;
import com.dk.dto.DtItemDTO;
import com.dk.em.ResultEnum;
import com.dk.entity.DtBrands;
import com.dk.entity.DtFans;
import com.dk.entity.DtItem;
import com.dk.entity.DtRecycleItem;
import com.dk.feign.GoodFeign;
import com.dk.service.IDtBrandsService;
import com.dk.service.IDtRecycleItemService;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("nearbyController")
@Api(value = "附近回收")
public class NearbyController {
    @Autowired
    private GoodFeign goodFeign;
    @Autowired
    private IDtBrandsService brandsService;
    @Resource
    private IDtRecycleItemService recycleItemService;
    @PostMapping("getCity")
    @ApiIgnore(value = "城市")
    public Result getCity() throws URISyntaxException {
        String ak = "hGxUITgDcUSjS0gCBN5fN3KSwO2f7jvI";
        String url = "https://api.map.baidu.com/location/ip";
        Map<String,Object> map  = new HashMap<>();
        map.put("ak",ak);
        String info = getInfo(map, url);
        System.out.println(info);
        return Result.response(ResultEnum.SUCCESS).setData(info);
    }
    @PostMapping("city")
    @ApiIgnore(value = "附近商品展示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "word",value = "输入搜索关键词",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageNumber",value = "当前页",required = true,paramType = "query"),
    })
    public Result nearbyProduct(@RequestParam(defaultValue = "1")Integer pageNumber,
                                @RequestParam(defaultValue = "3")Integer pageSize,
                                @RequestParam("word")String word){
        Result<DtItemDTO> word1 = goodFeign.getWord(pageNumber, pageSize, word);
        return word1;
    }
    @PostMapping("stickRecycle")
    @ApiIgnore(value = "单件回收")
    public Result stickRecycle(){
        List<DtBrands> brandsList =  brandsService.selectList();
        return Result.response(ResultEnum.SUCCESS).setData(brandsList);
    }
    @PostMapping("stickSame")
    @ApiIgnore(value = "卖同款")
    public Result stickRecycle2(){
        //通过Feign调用省心卖(高价回收）的接口，回收单件商品
        return Result.response(ResultEnum.SUCCESS);
    }
    @PostMapping("cardRecycle")
    @ApiIgnore(value = "卡卷回收")
    public Result cardRecycle(){
        List<DtRecycleItem> recycleList =  recycleItemService.cardRecycleList();
        return Result.response(ResultEnum.SUCCESS).setData(recycleList);
    }
    @PostMapping("collectRecycle")
    @ApiIgnore(value = "收藏品回收")
    public Result collectRecycle(){
        List<DtRecycleItem> recycleList =  recycleItemService.collectRecycleList();
        return Result.response(ResultEnum.SUCCESS).setData(recycleList);
    }
    @PostMapping("otherRecycle")
    @ApiIgnore(value = "其它回收")
    public Result otherRecycle(){
        List<DtRecycleItem> recycleList =  recycleItemService.otherRecycleList();
        return Result.response(ResultEnum.SUCCESS).setData(recycleList);
    }

    public String getInfo(Map<String, Object> map, String url) throws URISyntaxException {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet get = null;
        if (map.keySet().size() != 0) {
            URIBuilder builder = new URIBuilder(url);
            Set<String> strings = map.keySet();
            for (String string : strings) {
                if (map.get(string) != null) {
                    builder.setParameter(string, map.get(string).toString());
                }
            }
            get = new HttpGet(builder.build());
        }else {
            get = new HttpGet(url);
        }
        CloseableHttpResponse response = null;
        try {
            response = client.execute(get);
            String s = EntityUtils.toString(response.getEntity(), "utf-8");
            return s;

        }catch (Exception e) {
            e.printStackTrace();
            return null;

        }finally {
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


